﻿using MediatR;
using SmartCity.ProcessEngine.Api.Read.Application.Command.Instance;
using SmartCity.ProcessEngine.Api.Read.Dtos;
using SmartCity.ProcessEngine.Domain.Entities;
using SmartCity.ProcessEngine.ErrorCode;
using SmartCity.ProcessEngine.Infrastructure.Interface;

namespace SmartCity.ProcessEngine.Api.Read.Application.CommandHandler.Instance
{
    public class GetRecardListHandler : IRequestHandler<GetRecardListCommand, ApiResult<List<RecardListDTO>>>
    {
        private readonly IBaseRepository<InstanceRecardModel> recardRepo;

        public GetRecardListHandler(IBaseRepository<InstanceRecardModel> recardRepo)
        {
            this.recardRepo = recardRepo;
        }
        public async Task<ApiResult<List<RecardListDTO>>> Handle(GetRecardListCommand request, CancellationToken cancellationToken)
        {
            var linq = from r in recardRepo.GetAll().Where(x => x.IntanceId == request.InstanceId && x.IsDeleted == false)
                       select new RecardListDTO { 
                         Id =r.Id,
                          ApproveStatus = r.ApproveStatus,
                           CreatedTime=r.CreatedTime,
                            NodeName = r.NodeName,
                             OperatorName = r.OperatorName,
                              OperatorOpinion = r.OperatorOpinion
                       };

            return new ApiResult<List<RecardListDTO>>().Success("查询审批列表成功!", linq.ToList());
        }
    }
}
